package com.climber.spb_shiro_custable.controller;

import com.climber.spb_shiro_custable.services.UserServiceImpl;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Random;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    @RequestMapping("loginCheck")
    public String login(String username,String password,Boolean rememberMe){
        if (null==rememberMe){
            rememberMe=false;
        }
        try {
            userService.checkLogin(username,password,rememberMe);
            return "/index";
        } catch (Exception e) {
            System.out.println("密码错误");
            return "login";
        }
    }

    @RequestMapping("register")
    public String register(@RequestParam("username") String username, @RequestParam("password")String password,@RequestParam("rid")String rid) {
        System.out.println(rid);
        Md5Hash md5Hash = new Md5Hash(password);
        int salt = new Random().nextInt(90000) + 10000;  //10000-99999
        //加盐加密+Hash次数
        Md5Hash md5Hash2 = new Md5Hash(password, salt + "", 1);
        int a=userService.addUser(username, md5Hash2.toString(),(salt+""),rid);
        return "login";
    }




}



